package com.slashmobility.finappsparty.view;

import com.slashmobility.finapps.model.DataModelValue;
import com.slashmobility.finappsparty.helper.DateHelper;
import com.slashmobility.finappsparty.helper.ViewsHelper;

import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableRow;
import android.widget.TextView;
import com.droidux.samples.trial.R;

/**
 * Conceptualiza una fila - layout a aplicar en una vista
 * @author josecortes
 *
 */
public class ValueLayout extends TableRow {

	private int defaultColor=0xffffffff;
 
	private TextView tvDescripcion = null;
	private TextView tvDif = null;
	private TextView tvPercent = null;
	private TextView tvUlt = null;
	
	private TextView tvAnt = null;
	private TextView tvRent = null;
 
	private TextView tvMax = null;
	private TextView tvMin = null;
	private TextView tvHora = null;
	

	private String codigo;
	private String descripcion;
	private String dif;
	private String percent;
	private String ult;
	
	//Valores modo landscape
	
	private String ant;
	private String rent;
	private String vol;
	private String max;
	private String min;
	private String hora;
	
	private DataModelValue dmv;
	
	public DataModelValue getDmv() {
		return dmv;
	}

	public void setDmv(DataModelValue dmv) {
		this.dmv = dmv;
	}



	private OnClickListener onClickDetallesValor=null;
	
	public OnClickListener getOnClickDetallesValor() {
		return onClickDetallesValor;
	}

	public void setOnClickDetallesValor(OnClickListener onClickDetallesValor) {
		this.onClickDetallesValor = onClickDetallesValor;
	}

	public int getDefaultColor() {
		return defaultColor;
	}

	public void setDefaultColor(int defaultColor) {
		this.defaultColor = defaultColor;
	}

	public ValueLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		
		LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);		 
		setLayoutParams(params);
	}

	public String getCodigo() {
		return codigo;
	}

	public void setCodigo(String codigo) {
		this.codigo = codigo;
	}

	public String getDescripcion() {
		return descripcion;
	}

	public void setDescripcion(String descripcion) {
		this.descripcion = descripcion;
	}

	public String getDif() {
		return dif;
	}

	public void setDif(String dif) {
		this.dif = dif;
	}

	public String getPercent() {
		return percent;
	}

	public void setPercent(String percent) {
		this.percent = percent;
	}

	public String getUlt() {
		return ult;
	}

	public void setUlt(String ult) {
		this.ult = ult;
	}

	

	
	
	//Valores a mostrar
	
	public String getAnt() {
		return ant;
	}

	public void setAnt(String ant) {
		this.ant = ant;
	}

	public String getRent() {
		return rent;
	}

	public void setRent(String rent) {
		this.rent = rent;
	}

	public String getVol() {
		return vol;
	}

	public void setVol(String vol) {
		this.vol = vol;
	}

	public String getMax() {
		return max;
	}

	public void setMax(String max) {
		this.max = max;
	}

	public String getMin() {
		return min;
	}

	public void setMin(String min) {
		this.min = min;
	}

	public String getHora() {
		return hora;
	}

	public void setHora(String hora) {
		this.hora = hora;
	}


	
	private Context context;

	@Override
	public void addView(View child) {
		// TODO Auto-generated method stub
		super.addView(child);
	}

	@Override
	public void addView(View child, int index) {
		super.addView(child, index);
	}

	public void insertValue(DataModelValue dataModelValue) {
		codigo = dataModelValue.getCodigo();
		descripcion = dataModelValue.getDescripcion();
		dif = dataModelValue.getDif();
		percent = ViewsHelper.roundFloat(dataModelValue.getPercent());
		ult = ViewsHelper.roundFloat(dataModelValue.getUlt());
		ant= ViewsHelper.roundFloat(dataModelValue.getAnt());
		rent=ViewsHelper.roundFloat(dataModelValue.getRentabilidad());
		vol=ViewsHelper.roundFloat(dataModelValue.getVolumen());
		max=ViewsHelper.roundFloat(dataModelValue.getMax());
		min=ViewsHelper.roundFloat(dataModelValue.getMin());
		hora=DateHelper.roundTime(dataModelValue.getHora());
		hora=hora==null?"N/A":hora;
		vol=vol==null?"N/A":vol;
		
		//tvCodigo = createTextView(codigo);
		//addView(tvCodigo, 0);
		
		int rotation = getResources().getConfiguration().orientation;
		
		if (rotation == Configuration.ORIENTATION_PORTRAIT) {
			tvDescripcion = createTextView(descripcion, LayoutParams.WRAP_CONTENT,0f,ViewsHelper.dipToPixels(context, 108), Gravity.LEFT);
			addView(tvDescripcion, 0);
			
			
			tvUlt = createTextView(ViewsHelper.roundFloat(ult) + "", LayoutParams.WRAP_CONTENT,0f,ViewsHelper.dipToPixels(context, 56 ), Gravity.RIGHT);
			addView(tvUlt, 1);
			tvDif = createTextView(ViewsHelper.roundFloat(dif) + "", LayoutParams.WRAP_CONTENT,0f,ViewsHelper.dipToPixels(context, 50), Gravity.RIGHT);
			addView(tvDif, 2);
			tvPercent = createTextView(ViewsHelper.roundFloat(percent) + "", LayoutParams.FILL_PARENT,1f,ViewsHelper.dipToPixels(context, 0), Gravity.RIGHT);
			addView(tvPercent, 3);

			
			ImageView imgMasDetalles=createMasDetallesView();
			addView(imgMasDetalles,4);
		}
		 
	}


	public void updateValue(DataModelValue dataModelValue) {
		
		
		updateNumericalTextView(tvDif, dif, ViewsHelper.roundFloat(dataModelValue.getDif()));
		updateNumericalTextView(tvPercent, percent, ViewsHelper.roundFloat(dataModelValue.getPercent()));
		updateNumericalTextView(tvUlt,ult, ViewsHelper.roundFloat(dataModelValue.getUlt()));
		
		updateNumericalTextView(tvAnt,ant, ViewsHelper.roundFloat(dataModelValue.getAnt()));		 
		updateNumericalTextView(tvMax,max, ViewsHelper.roundFloat(dataModelValue.getMax()));
		updateNumericalTextView(tvMin,min, ViewsHelper.roundFloat(dataModelValue.getMin()));
		updateNumericalTextView(tvRent,rent, ViewsHelper.roundFloat(dataModelValue.getRentabilidad()));

		
		// codigo = dataModelValue.getCodigo();
		// descripcion = dataModelValue.getDescripcion();
		dif = ViewsHelper.roundFloat(dataModelValue.getDif());
		percent = ViewsHelper.roundFloat(dataModelValue.getPercent());
		ult = ViewsHelper.roundFloat(dataModelValue.getUlt());
		
		ant = ViewsHelper.roundFloat(dataModelValue.getAnt());
		max=ViewsHelper.roundFloat(dataModelValue.getMax());
		min = ViewsHelper.roundFloat(dataModelValue.getMin());
		rent=ViewsHelper.roundFloat(dataModelValue.getRentabilidad());

	}

	private void updateNumericalTextView(TextView tv, String currentDataStr,
			String newDataStr) {
		
		if(currentDataStr.equalsIgnoreCase("n/a") || newDataStr.equalsIgnoreCase("n/a"))
			return;
		if (tv == null)
			return;
		
		float currentData=Float.parseFloat(currentDataStr);
		float newData=Float.parseFloat(newDataStr);
		
		int color = 0;
		if (newData > currentData)
			color = getResources().getColor(R.color.la_caixa_green);
		else if (newData == currentData)
			color = defaultColor;
		else if (newData < currentData)
			color = getResources().getColor(R.color.la_caixa_orange);;
		
		tv.setText(newData+"");
		tv.setBackgroundColor(color);
		tv.invalidate();

	}

	private TextView createTextView(String data, int layoutWidth, float weight, float widthView, int gravity) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		TextView textView = (TextView) inflater.inflate(R.layout.layout_rowtextvalue,
				null);
		// TextView textView = new TextView(context);
		String filteredData=data.length()>13?data.substring(0,13):data;
		textView.setText(filteredData);

		TableRow.LayoutParams params = new TableRow.LayoutParams(
				layoutWidth, LayoutParams.WRAP_CONTENT, weight);
		params.setMargins(1, 0, 1, 0);
		textView.setLayoutParams(params);
		textView.setBackgroundColor(defaultColor);
		textView.setPadding(2, 14, 2, 14);
		textView.setGravity(gravity);
		if(widthView!=0.0)
		{
			textView.setWidth((int)widthView);
		}
		return textView;
	}
	
	private ImageView createMasDetallesView()
	{
		ImageView imgMasDetalles=new ImageView(context);
		TableRow.LayoutParams params = new TableRow.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		params.setMargins(0, 0, 0, 0);
		
		imgMasDetalles.setLayoutParams(params);
		imgMasDetalles.setImageResource(R.drawable.img_detallevalor);
		imgMasDetalles.setBackgroundColor(defaultColor);
		imgMasDetalles.setPadding(10, 0, 10, 0);
		imgMasDetalles.setOnClickListener(onClickDetallesValor);
		return imgMasDetalles;
	}

}
